package com.hp.common.validator;

import com.hp.common.annotation.FlagValidator;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class FlagValidatorClass implements ConstraintValidator<FlagValidator,Object> {

    //保存flag值
    private String values;
    private boolean required;

    @Override
    public void initialize(FlagValidator flagValidator) {
        this.values = flagValidator.values();
        this.required = flagValidator.required();
    }

    @Override
    public boolean isValid(Object value, ConstraintValidatorContext constraintValidatorContext) {
        if(this.required){
            if (null == value || "".equals(value)){
                return false;
            }
        }
        //切割获取值
        String [] valueArray = values.split(",");
        Boolean isFlag = false;
        for (int i = 0; i < valueArray.length; i++) {
            //存在一致就跳出循环
            if(valueArray[i].equals(value.toString())){
                isFlag = true;
                break;
            }

        }
        return isFlag;
    }
}
